home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / dev / misc / libx11.lha / libX11 / timing.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-22  |  4.5 KB  |  178 lines

  1. /* Copyright (c) 1996 by Terje Pedersen.  All Rights Reserved   */
  2. /*                                                              */
  3. /* By using this code you will agree to these terms:            */
  4. /*                                                              */
  5. /* 1. You may not use this code for profit in any way or form   */
  6. /*    unless an agreement with the author has been reached.     */
  7. /*                                                              */
  8. /* 2. The author is not responsible for any damages caused by   */
  9. /*    the use of this code.                                     */
  10. /*                                                              */
  11. /* 3. All modifications are to be released to the public.       */
  12. /*                                                              */
  13. /* Thats it! Have fun!                                          */
  14. /* TP                                                           */
  15. /*                                                              */
  16.  
  17. /***
  18.    NAME
  19.      timing
  20.    PURPOSE
  21.      
  22.    NOTES
  23.      
  24.    HISTORY
  25.      Terje Pedersen - Dec 17, 1994: Created.
  26. **/
  27.  
  28. #include <exec/io.h>
  29. #include <devices/timer.h>
  30.  
  31. #include <clib/exec_protos.h>
  32. #include <clib/alib_protos.h>
  33. #include <clib/dos_protos.h>
  34. #include <clib/intuition_protos.h>
  35.  
  36. #include <stdio.h>
  37.  
  38. #include "libX11.h"
  39.  
  40. #include <X11/X.h>
  41. #include <X11/Xlib.h>
  42.  
  43. #include "amigax_proto.h"
  44. #include "amiga_x.h"
  45.  
  46. extern struct DosLibrary *DOSBase;
  47. struct Library *TimerBase=NULL;
  48.  
  49. void wait_for_timer(struct timerequest *, struct timeval *);
  50.  
  51. void delete_timer(struct timerequest *tr ){
  52.   struct MsgPort *tp;
  53.   
  54.   if (tr != 0 ){
  55.     tp = tr->tr_node.io_Message.mn_ReplyPort;
  56.     if (tp != 0) DeletePort(tp);
  57.  
  58.     CloseDevice( (struct IORequest *) tr );
  59.     DeleteExtIO( (struct IORequest *) tr );
  60.   }
  61. }
  62.  
  63. struct timerequest *create_timer( ULONG unit ){
  64.   /* return a pointer to a timer request.  If any problem, return NULL */
  65.   LONG error;
  66.   struct MsgPort *timerport;
  67.   struct timerequest *TimerIO;
  68.   
  69.   timerport = CreatePort( 0, 0 );
  70.   if (timerport == NULL ) return( NULL );
  71.  
  72.   TimerIO = (struct timerequest *)
  73.     CreateExtIO( timerport, sizeof( struct timerequest ) );
  74.   if (TimerIO == NULL ){
  75.     DeletePort(timerport);   /* Delete message port */
  76.     return( NULL );
  77.   }
  78.   
  79.   error = OpenDevice( TIMERNAME, unit,(struct IORequest *) TimerIO, 0L );
  80.   if (error != 0 ){
  81.     delete_timer( TimerIO );
  82.     return( NULL );
  83.   }
  84.   return( TimerIO );
  85. }
  86.  
  87. /* more precise timer than AmigaDOS Delay() */
  88. LONG time_delay( struct timeval *tv, LONG unit ){
  89.   struct timerequest *tr;
  90.   /* get a pointer to an initialized timer request block */
  91.   tr = create_timer( unit );
  92.  
  93.   /* any nonzero return says timedelay routine didn't work. */
  94.   if (tr == NULL ) return( -1L );
  95.  
  96.   wait_for_timer( tr, tv );
  97.  
  98.   /* deallocate temporary structures */
  99.   delete_timer( tr );
  100.   return( 0L );
  101. }
  102.  
  103. void wait_for_timer(struct timerequest *tr, struct timeval *tv ){
  104.  
  105.   tr->tr_node.io_Command = TR_ADDREQUEST; /* add a new timer request */
  106.  
  107.   /* structure assignment */
  108.   tr->tr_time = *tv;
  109.  
  110.   /* post request to the timer -- will go to sleep till done */
  111.   DoIO((struct IORequest *) tr );
  112. }
  113.  
  114.  
  115. struct MsgPort *replymp;
  116. struct timerequest *tr;
  117. BOOL not_opened = TRUE;
  118.  
  119. int open_timer(){
  120.   struct timerequest *xtr;
  121.   if(TimerBase)return;
  122.   replymp = (struct MsgPort *) CreatePort( NULL, 0 );
  123.   if( !replymp ){
  124.     return(0);
  125.   }
  126.   tr = (struct timerequest *)
  127.     CreateExtIO( replymp, sizeof( struct timerequest) );
  128.   if( !tr ){
  129.     return(0);
  130.   }
  131.  
  132.   not_opened = OpenDevice( TIMERNAME, UNIT_ECLOCK, (struct IORequest *)tr ,0L);
  133.   if( not_opened ){
  134.     return(0);
  135.   }
  136.   /* get a pointer to an initialized timer request block */
  137.   xtr = create_timer( UNIT_MICROHZ );
  138.   TimerBase = (struct Library *)xtr->tr_node.io_Device;
  139.   delete_timer( xtr );
  140.   return(1);
  141. }
  142.  
  143. void close_timer(){
  144.   if( tr ){
  145.     if( !not_opened ) CloseDevice( (struct IORequest *)tr );
  146.     DeleteExtIO( (struct IORequest*)tr /*, sizeof( struct timerequest)*/ );
  147.     tr=NULL;
  148.   }
  149.   if( replymp ) DeletePort( replymp);
  150.   replymp=NULL;
  151. }
  152.  
  153. #define CLK_TCK 1000
  154.  
  155. long clock_ticks(){
  156.   tr->tr_node.io_Command = TR_GETSYSTIME;
  157.   DoIO( (struct IORequest *)tr );
  158.   return((long)(tr->tr_time.tv_secs*CLK_TCK+(long)(tr->tr_time.tv_micro/1000)));
  159. }
  160.  
  161. void X11delayfor( int sec, int micro ){
  162.   struct timeval currentval;
  163.   currentval.tv_secs = sec;
  164.   currentval.tv_micro = micro;
  165.   time_delay( ¤tval, UNIT_MICROHZ );
  166. }
  167.  
  168. usleep(usecs){
  169.   struct timeval currentval;
  170.   currentval.tv_secs = 0;
  171.   currentval.tv_micro = usecs;
  172.   time_delay( ¤tval, UNIT_MICROHZ );
  173. }
  174.  
  175. sleep(secs){
  176.   usleep(1000*secs);
  177. }
  178.